﻿Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Runtime.CompilerServices
Imports System.Workflow.ComponentModel.Design
Imports System.Workflow.ComponentModel.Serialization

Namespace System.Workflow.ComponentModel
    <DesignerSerializer(GetType(DependencyObjectCodeDomSerializer), GetType(CodeDomSerializer)), DesignerSerializer(GetType(WorkflowMarkupSerializer), GetType(WorkflowMarkupSerializer))> _
    Public MustInherit Class DependencyObject
        Implements IComponent, IDependencyObjectAccessor, IDisposable
        ' Events
        Private Custom Event disposed As EventHandler
        Private Custom Event System.ComponentModel.IComponent.Disposed As EventHandler

        ' Methods
        Protected Sub New()
            Me.SetReadOnlyPropertyValue(DependencyObject.ReadonlyProperty, False)
            Me.readonlyPropertyValue = False
            Me.SetReadOnlyPropertyValue(DependencyObject.UserDataProperty, Hashtable.Synchronized(New Hashtable))
        End Sub

        Public Sub [AddHandler](ByVal dependencyEvent As DependencyProperty, ByVal value As Object)
            If (dependencyEvent Is Nothing) Then
                Throw New ArgumentNullException("dependencyEvent")
            End If
            If (value Is Nothing) Then
                Throw New ArgumentNullException("value")
            End If
            If TypeOf value Is ActivityBind Then
                Throw New ArgumentException(SR.GetString("Error_DPSetValueBind"), "value")
            End If
            If ((Not dependencyEvent.DefaultMetadata Is Nothing) AndAlso dependencyEvent.DefaultMetadata.IsMetaProperty) Then
                Throw New ArgumentException(SR.GetString("Error_DPAddHandlerMetaProperty"), "dependencyEvent")
            End If
            If Not dependencyEvent.IsEvent Then
                Throw New ArgumentException(SR.GetString("Error_DPAddHandlerNonEvent"), "dependencyEvent")
            End If
            If (dependencyEvent.PropertyType Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_DPPropertyTypeMissing"), "dependencyEvent")
            End If
            If (dependencyEvent.OwnerType Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_MissingOwnerTypeProperty"), "dependencyEvent")
            End If
            If (Not dependencyEvent.IsAttached AndAlso Not dependencyEvent.OwnerType.IsAssignableFrom(MyBase.GetType)) Then
                Throw New InvalidOperationException(SR.GetString("Error_InvalidDependencyProperty", New Object() { MyBase.GetType.FullName, dependencyEvent.Name, dependencyEvent.OwnerType.FullName }))
            End If
            If ((Not value Is Nothing) AndAlso Not dependencyEvent.PropertyType.IsAssignableFrom(value.GetType)) Then
                Throw New ArgumentException(SR.GetString("Error_DynamicPropertyTypeValueMismatch", New Object() { dependencyEvent.PropertyType.FullName, dependencyEvent.Name, value.GetType.FullName }), "value")
            End If
            Dim dependencyPropertyValues As IDictionary([Of] DependencyProperty, Object) = Me.DependencyPropertyValues
            Dim list As ArrayList = Nothing
            If dependencyPropertyValues.ContainsKey(dependencyEvent) Then
                list = DirectCast(dependencyPropertyValues.Item(dependencyEvent), ArrayList)
            Else
                list = New ArrayList
                dependencyPropertyValues.Add(dependencyEvent, list)
            End If
            list.Add(value)
            If (Me.DesignMode AndAlso Me.metaProperties.ContainsKey(dependencyEvent)) Then
                Me.metaProperties.Remove(dependencyEvent)
            End If
        End Sub

        Public Sub Dispose()
            Me.Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If ((Not Me.Site Is Nothing) AndAlso (Not Me.Site.Container Is Nothing)) Then
                    Me.Site.Container.Remove(Me)
                End If
                If (Not Me.disposed Is Nothing) Then
                    Me.disposed.Invoke(Me, EventArgs.Empty)
                End If
            End If
        End Sub

        Protected Overrides Sub Finalize()
            Try 
                Me.Dispose(False)
            Finally
                MyBase.Finalize
            End Try
        End Sub

        Friend Overridable Sub FixUpMetaProperties(ByVal originalObject As DependencyObject)
            If (originalObject Is Nothing) Then
                Throw New ArgumentNullException
            End If
            Me.metaProperties = originalObject.metaProperties
            Me.readonlyPropertyValue = True
            Dim pair As KeyValuePair([Of] DependencyProperty, Object)
            For Each pair In Me.DependencyPropertyValues
                If ((Not pair.Key Is DependencyObject.ParentDependencyObjectProperty) AndAlso originalObject.DependencyPropertyValues.ContainsKey(pair.Key)) Then
                    Dim obj2 As Object = originalObject.DependencyPropertyValues.Item(pair.Key)
                    If TypeOf pair.Value Is DependencyObject Then
                        DirectCast(pair.Value, DependencyObject).FixUpMetaProperties([TryCast](obj2,DependencyObject))
                    ElseIf TypeOf pair.Value Is WorkflowParameterBindingCollection Then
                        Dim list As IList = [TryCast](pair.Value,IList)
                        Dim list2 As IList = [TryCast](obj2,IList)
                        Dim i As Integer
                        For i = 0 To list.Count - 1
                            DirectCast(list.Item(i), DependencyObject).FixUpMetaProperties([TryCast](list2.Item(i),DependencyObject))
                        Next i
                    End If
                End If
            Next
        End Sub

        Public Function GetBinding(ByVal dependencyProperty As DependencyProperty) As ActivityBind
            If (dependencyProperty Is Nothing) Then
                Throw New ArgumentNullException("dependencyProperty")
            End If
            If Not Me.metaProperties.ContainsKey(dependencyProperty) Then
                Return Nothing
            End If
            Return [TryCast](Me.metaProperties.Item(dependencyProperty),ActivityBind)
        End Function

        <EditorBrowsable(EditorBrowsableState.Never)> _
        Protected Overridable Function GetBoundValue(ByVal bind As ActivityBind, ByVal targetType As Type) As Object
            If (bind Is Nothing) Then
                Throw New ArgumentNullException("bind")
            End If
            If (targetType Is Nothing) Then
                Throw New ArgumentNullException("targetType")
            End If
            Dim runtimeValue As Object = bind
            Dim activity As Activity = Me.ResolveOwnerActivity
            If (Not activity Is Nothing) Then
                runtimeValue = bind.GetRuntimeValue(activity, targetType)
            End If
            Return runtimeValue
        End Function

        Friend Function GetHandler(ByVal dependencyEvent As DependencyProperty) As Object
            If (dependencyEvent Is Nothing) Then
                Throw New ArgumentNullException("dependencyEvent")
            End If
            If Not dependencyEvent.IsEvent Then
                Throw New ArgumentException("dependencyEvent")
            End If
            Dim dependencyPropertyValues As IDictionary([Of] DependencyProperty, Object) = Me.DependencyPropertyValues
            If dependencyPropertyValues.ContainsKey(dependencyEvent) Then
                Dim list As ArrayList = [TryCast](dependencyPropertyValues.Item(dependencyEvent),ArrayList)
                If ((Not list Is Nothing) AndAlso (list.Count <> 0)) Then
                    If (list.Count = 1) Then
                        Return list.Item(0)
                    End If
                    Dim a As Delegate = [TryCast](list.Item(0),Delegate)
                    Dim i As Integer
                    For i = 1 To list.Count - 1
                        a = Delegate.Combine(a, [TryCast](list.Item(i),Delegate))
                    Next i
                    Return a
                End If
            End If
            Return Nothing
        End Function

        Protected Function GetInvocationList([Of] T)(ByVal dependencyEvent As DependencyProperty) As T()
            If (dependencyEvent Is Nothing) Then
                Throw New ArgumentNullException("dependencyEvent")
            End If
            If Not dependencyEvent.IsEvent Then
                Throw New ArgumentException(SR.GetString("Error_DPAddHandlerNonEvent"), "dependencyEvent")
            End If
            Dim dependencyPropertyValues As IDictionary([Of] DependencyProperty, Object) = Nothing
            If Me.DependencyPropertyValues.ContainsKey(dependencyEvent) Then
                dependencyPropertyValues = Me.DependencyPropertyValues
            Else
                dependencyPropertyValues = Me.metaProperties
            End If
            Dim list As New List([Of] T)
            If dependencyPropertyValues.ContainsKey(dependencyEvent) Then
                If TypeOf dependencyPropertyValues.Item(dependencyEvent) Is ActivityBind Then
                    If Not Me.DesignMode Then
                        Dim item As T = CType(Nothing, T)
                        item = DirectCast(Me.GetBoundValue(DirectCast(dependencyPropertyValues.Item(dependencyEvent), ActivityBind), GetType(T)), T)
                        If (Not item Is Nothing) Then
                            list.Add(item)
                        End If
                    End If
                Else
                    Dim obj2 As Object
                    For Each obj2 In DirectCast(dependencyPropertyValues.Item(dependencyEvent), ArrayList)
                        If TypeOf obj2 Is T Then
                            list.Add(DirectCast(obj2, T))
                        End If
                    Next
                End If
            End If
            Return list.ToArray
        End Function

        Public Function GetValue(ByVal dependencyProperty As DependencyProperty) As Object
            If (dependencyProperty Is Nothing) Then
                Throw New ArgumentNullException("dependencyProperty")
            End If
            If dependencyProperty.IsEvent Then
                Throw New ArgumentException(SR.GetString("Error_DPGetValueHandler"), "dependencyProperty")
            End If
            Dim defaultMetadata As PropertyMetadata = dependencyProperty.DefaultMetadata
            If (Not defaultMetadata.GetValueOverride Is Nothing) Then
                Return defaultMetadata.GetValueOverride.Invoke(Me)
            End If
            Return Me.GetValueCommon(dependencyProperty, defaultMetadata)
        End Function

        <EditorBrowsable(EditorBrowsableState.Never)> _
        Public Function GetValueBase(ByVal dependencyProperty As DependencyProperty) As Object
            If (dependencyProperty Is Nothing) Then
                Throw New ArgumentNullException("dependencyProperty")
            End If
            Return Me.GetValueCommon(dependencyProperty, dependencyProperty.DefaultMetadata)
        End Function

        Private Function GetValueCommon(ByVal dependencyProperty As DependencyProperty, ByVal metadata As PropertyMetadata) As Object
            Dim boundValue As Object
            If (Not Me.DependencyPropertyValues.TryGetValue(dependencyProperty, boundValue) AndAlso ((Me.metaProperties Is Nothing) OrElse Not Me.metaProperties.TryGetValue(dependencyProperty, boundValue))) Then
                Return dependencyProperty.DefaultMetadata.DefaultValue
            End If
            If (((Not Me.metaProperties Is Nothing) AndAlso Not Me.DesignMode) AndAlso (TypeOf boundValue Is ActivityBind AndAlso Not GetType(ActivityBind).IsAssignableFrom(dependencyProperty.PropertyType))) Then
                boundValue = Me.GetBoundValue(DirectCast(boundValue, ActivityBind), dependencyProperty.PropertyType)
            End If
            If ((boundValue Is Nothing) OrElse TypeOf boundValue Is ActivityBind) Then
                Return dependencyProperty.DefaultMetadata.DefaultValue
            End If
            If Not dependencyProperty.PropertyType.IsAssignableFrom(boundValue.GetType) Then
                Throw New InvalidOperationException(SR.GetString("Error_DynamicPropertyTypeValueMismatch", New Object() { dependencyProperty.PropertyType.FullName, dependencyProperty.Name, boundValue.GetType.FullName }))
            End If
            Return boundValue
        End Function

        Protected Overridable Sub InitializeProperties()
        End Sub

        Public Function IsBindingSet(ByVal dependencyProperty As DependencyProperty) As Boolean
            If (dependencyProperty Is Nothing) Then
                Throw New ArgumentNullException("dependencyProperty")
            End If
            Return ((Not Me.DependencyPropertyValues.ContainsKey(dependencyProperty) AndAlso Me.metaProperties.ContainsKey(dependencyProperty)) AndAlso TypeOf Me.metaProperties.Item(dependencyProperty) Is ActivityBind)
        End Function

        Public Function MetaEquals(ByVal dependencyObject As DependencyObject) As Boolean
            Return ((Not dependencyObject Is Nothing) AndAlso (dependencyObject.metaProperties Is Me.metaProperties))
        End Function

        Friend Overridable Sub OnInitializeActivatingInstanceForRuntime(ByVal workflowCoreRuntime As IWorkflowCoreRuntime)
            Me.InitializeProperties
        End Sub

        Friend Overridable Sub OnInitializeDefinitionForRuntime()
        End Sub

        Friend Overridable Sub OnInitializeInstanceForRuntime(ByVal workflowCoreRuntime As IWorkflowCoreRuntime)
        End Sub

        Public Sub [RemoveHandler](ByVal dependencyEvent As DependencyProperty, ByVal value As Object)
            If (dependencyEvent Is Nothing) Then
                Throw New ArgumentNullException("dependencyEvent")
            End If
            If (value Is Nothing) Then
                Throw New ArgumentNullException("value")
            End If
            If TypeOf value Is ActivityBind Then
                Throw New ArgumentException(SR.GetString("Error_DPRemoveHandlerBind"), "value")
            End If
            If ((Not dependencyEvent.DefaultMetadata Is Nothing) AndAlso dependencyEvent.DefaultMetadata.IsMetaProperty) Then
                Throw New ArgumentException(SR.GetString("Error_DPAddHandlerMetaProperty"), "dependencyEvent")
            End If
            If Not dependencyEvent.IsEvent Then
                Throw New ArgumentException(SR.GetString("Error_DPAddHandlerNonEvent"), "dependencyEvent")
            End If
            Dim dependencyPropertyValues As IDictionary([Of] DependencyProperty, Object) = Me.DependencyPropertyValues
            If dependencyPropertyValues.ContainsKey(dependencyEvent) Then
                Dim list As ArrayList = DirectCast(dependencyPropertyValues.Item(dependencyEvent), ArrayList)
                If list.Contains(value) Then
                    list.Remove(value)
                End If
                If (list.Count = 0) Then
                    dependencyPropertyValues.Remove(dependencyEvent)
                End If
            End If
        End Sub

        Public Function RemoveProperty(ByVal dependencyProperty As DependencyProperty) As Boolean
            If (dependencyProperty Is Nothing) Then
                Throw New ArgumentNullException("dependencyProperty")
            End If
            If ((Not dependencyProperty.DefaultMetadata Is Nothing) AndAlso dependencyProperty.DefaultMetadata.IsMetaProperty) Then
                If Not Me.DesignMode Then
                    Throw New InvalidOperationException(SR.GetString("Error_CanNotChangeAtRuntime"))
                End If
                Return Me.metaProperties.Remove(dependencyProperty)
            End If
            Return (Me.metaProperties.Remove(dependencyProperty) Or Me.DependencyPropertyValues.Remove(dependencyProperty))
        End Function

        Private Function ResolveOwnerActivity() As Activity
            Dim parentDependencyObject As DependencyObject = Me
            Do While ((Not parentDependencyObject Is Nothing) AndAlso Not TypeOf parentDependencyObject Is Activity)
                parentDependencyObject = parentDependencyObject.ParentDependencyObject
            Loop
            Return [TryCast](parentDependencyObject,Activity)
        End Function

        Public Sub SetBinding(ByVal dependencyProperty As DependencyProperty, ByVal bind As ActivityBind)
            If (dependencyProperty Is Nothing) Then
                Throw New ArgumentNullException("dependencyProperty")
            End If
            If ((Not dependencyProperty.DefaultMetadata Is Nothing) AndAlso dependencyProperty.DefaultMetadata.IsReadOnly) Then
                Throw New ArgumentException(SR.GetString("Error_DPReadOnly"), "dependencyProperty")
            End If
            If (dependencyProperty.OwnerType Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_MissingOwnerTypeProperty"), "dependencyProperty")
            End If
            If (Not dependencyProperty.IsAttached AndAlso Not dependencyProperty.OwnerType.IsAssignableFrom(MyBase.GetType)) Then
                Throw New InvalidOperationException(SR.GetString("Error_InvalidDependencyProperty", New Object() { MyBase.GetType.FullName, dependencyProperty.Name, dependencyProperty.OwnerType.FullName }))
            End If
            If Not Me.DesignMode Then
                Throw New InvalidOperationException(SR.GetString("Error_CanNotChangeAtRuntime"))
            End If
            If (((Not dependencyProperty.DefaultMetadata Is Nothing) AndAlso dependencyProperty.DefaultMetadata.IsMetaProperty) AndAlso Not GetType(ActivityBind).IsAssignableFrom(dependencyProperty.PropertyType)) Then
                Throw New ArgumentException(SR.GetString("Error_DPMetaPropertyBinding"), "dependencyProperty")
            End If
            If Me.metaProperties.ContainsKey(dependencyProperty) Then
                Me.metaProperties.Item(dependencyProperty) = bind
            Else
                Me.metaProperties.Add(dependencyProperty, bind)
            End If
            If Me.DependencyPropertyValues.ContainsKey(dependencyProperty) Then
                Me.DependencyPropertyValues.Remove(dependencyProperty)
            End If
        End Sub

        <EditorBrowsable(EditorBrowsableState.Never)> _
        Protected Overridable Sub SetBoundValue(ByVal bind As ActivityBind, ByVal value As Object)
            If (bind Is Nothing) Then
                Throw New ArgumentNullException("bind")
            End If
            Dim activity As Activity = Me.ResolveOwnerActivity
            If (Not activity Is Nothing) Then
                bind.SetRuntimeValue(activity, value)
            End If
        End Sub

        Protected Friend Sub SetReadOnlyPropertyValue(ByVal dependencyProperty As DependencyProperty, ByVal value As Object)
            If (dependencyProperty Is Nothing) Then
                Throw New ArgumentNullException("dependencyProperty")
            End If
            If Not dependencyProperty.DefaultMetadata.IsReadOnly Then
                Throw New InvalidOperationException(SR.GetString("Error_NotReadOnlyProperty", New Object() { dependencyProperty.Name, dependencyProperty.OwnerType.FullName }))
            End If
            If (Not dependencyProperty.IsAttached AndAlso Not dependencyProperty.OwnerType.IsAssignableFrom(MyBase.GetType)) Then
                Throw New InvalidOperationException(SR.GetString("Error_InvalidDependencyProperty", New Object() { MyBase.GetType.FullName, dependencyProperty.Name, dependencyProperty.OwnerType.FullName }))
            End If
            Dim metaProperties As IDictionary([Of] DependencyProperty, Object) = Nothing
            If dependencyProperty.DefaultMetadata.IsMetaProperty Then
                metaProperties = Me.metaProperties
            Else
                metaProperties = Me.DependencyPropertyValues
            End If
            If metaProperties.ContainsKey(dependencyProperty) Then
                metaProperties.Item(dependencyProperty) = value
            Else
                metaProperties.Add(dependencyProperty, value)
            End If
        End Sub

        Public Sub SetValue(ByVal dependencyProperty As DependencyProperty, ByVal value As Object)
            If (dependencyProperty Is Nothing) Then
                Throw New ArgumentNullException("dependencyProperty")
            End If
            Dim defaultMetadata As PropertyMetadata = dependencyProperty.DefaultMetadata
            Me.SetValueCommon(dependencyProperty, value, defaultMetadata, True)
        End Sub

        <EditorBrowsable(EditorBrowsableState.Never)> _
        Public Sub SetValueBase(ByVal dependencyProperty As DependencyProperty, ByVal value As Object)
            If (dependencyProperty Is Nothing) Then
                Throw New ArgumentNullException("dependencyProperty")
            End If
            Dim defaultMetadata As PropertyMetadata = dependencyProperty.DefaultMetadata
            Me.SetValueCommon(dependencyProperty, value, defaultMetadata, defaultMetadata.ShouldAlwaysCallOverride)
        End Sub

        Friend Sub SetValueCommon(ByVal dependencyProperty As DependencyProperty, ByVal value As Object, ByVal metadata As PropertyMetadata, ByVal shouldCallSetValueOverrideIfExists As Boolean)
            If dependencyProperty.DefaultMetadata.IsReadOnly Then
                Throw New ArgumentException(SR.GetString("Error_DPReadOnly"), "dependencyProperty")
            End If
            If TypeOf value Is ActivityBind Then
                Throw New ArgumentException(SR.GetString("Error_DPSetValueBind"), "value")
            End If
            If dependencyProperty.IsEvent Then
                Throw New ArgumentException(SR.GetString("Error_DPSetValueHandler"), "dependencyProperty")
            End If
            If (Not dependencyProperty.IsAttached AndAlso Not dependencyProperty.OwnerType.IsAssignableFrom(MyBase.GetType)) Then
                Throw New InvalidOperationException(SR.GetString("Error_InvalidDependencyProperty", New Object() { MyBase.GetType.FullName, dependencyProperty.Name, dependencyProperty.OwnerType.FullName }))
            End If
            If ((Not Me.DesignMode AndAlso dependencyProperty.DefaultMetadata.IsMetaProperty) AndAlso (Not dependencyProperty Is ConditionTypeConverter.DeclarativeConditionDynamicProp)) Then
                Throw New InvalidOperationException(SR.GetString("Error_CanNotChangeAtRuntime"))
            End If
            If ((Not value Is Nothing) AndAlso Not dependencyProperty.PropertyType.IsAssignableFrom(value.GetType)) Then
                Throw New ArgumentException(SR.GetString("Error_DynamicPropertyTypeValueMismatch", New Object() { dependencyProperty.PropertyType.FullName, dependencyProperty.Name, value.GetType.FullName }), "value")
            End If
            If (shouldCallSetValueOverrideIfExists AndAlso (Not metadata.SetValueOverride Is Nothing)) Then
                metadata.SetValueOverride.Invoke(Me, value)
            Else
                Dim metaProperties As IDictionary([Of] DependencyProperty, Object) = Nothing
                If dependencyProperty.DefaultMetadata.IsMetaProperty Then
                    metaProperties = Me.metaProperties
                Else
                    metaProperties = Me.DependencyPropertyValues
                End If
                Dim obj2 As Object = Nothing
                If ((Not Me.metaProperties Is Nothing) AndAlso Me.metaProperties.ContainsKey(dependencyProperty)) Then
                    obj2 = Me.metaProperties.Item(dependencyProperty)
                    If Me.DesignMode Then
                        Me.metaProperties.Remove(dependencyProperty)
                    End If
                End If
                If (Not Me.DesignMode AndAlso TypeOf obj2 Is ActivityBind) Then
                    Me.SetBoundValue(DirectCast(obj2, ActivityBind), value)
                ElseIf metaProperties.ContainsKey(dependencyProperty) Then
                    metaProperties.Item(dependencyProperty) = value
                Else
                    metaProperties.Add(dependencyProperty, value)
                End If
            End If
        End Sub

        Private Function System.Workflow.ComponentModel.IDependencyObjectAccessor.GetInvocationList([Of] T)(ByVal dependencyEvent As DependencyProperty) As T() Implements IDependencyObjectAccessor.GetInvocationList
            Return Me.GetInvocationList([Of] T)(dependencyEvent)
        End Function

        Private Sub System.Workflow.ComponentModel.IDependencyObjectAccessor.InitializeActivatingInstanceForRuntime(ByVal parentDependencyObject As DependencyObject, ByVal workflowCoreRuntime As IWorkflowCoreRuntime) Implements IDependencyObjectAccessor.InitializeActivatingInstanceForRuntime
            If (Not parentDependencyObject Is Nothing) Then
                Me.DependencyPropertyValues.Item(DependencyObject.ParentDependencyObjectProperty) = parentDependencyObject
            End If
            Dim property As DependencyProperty
            For Each property In Me.MetaDependencyProperties
                Dim obj2 As Object = Me.metaProperties.Item([property])
                If TypeOf obj2 Is DependencyObject Then
                    DirectCast(obj2, IDependencyObjectAccessor).InitializeActivatingInstanceForRuntime(Me, workflowCoreRuntime)
                    Me.DependencyPropertyValues.Item([property]) = obj2
                ElseIf TypeOf obj2 Is WorkflowParameterBindingCollection Then
                    Dim list As IList = [TryCast](obj2,IList)
                    Dim i As Integer
                    For i = 0 To list.Count - 1
                        DirectCast(list.Item(i), IDependencyObjectAccessor).InitializeActivatingInstanceForRuntime(Me, workflowCoreRuntime)
                    Next i
                    Me.DependencyPropertyValues.Item([property]) = obj2
                End If
            Next
            Me.OnInitializeActivatingInstanceForRuntime(workflowCoreRuntime)
            Me.Readonly = True
        End Sub

        Private Sub System.Workflow.ComponentModel.IDependencyObjectAccessor.InitializeDefinitionForRuntime(ByVal parentDependencyObject As DependencyObject) Implements IDependencyObjectAccessor.InitializeDefinitionForRuntime
            If (Not parentDependencyObject Is Nothing) Then
                Me.DependencyPropertyValues.Item(DependencyObject.ParentDependencyObjectProperty) = parentDependencyObject
            End If
            Dim property As DependencyProperty
            For Each property In Me.MetaDependencyProperties
                Dim obj2 As Object = Me.metaProperties.Item([property])
                If TypeOf obj2 Is DependencyObject Then
                    DirectCast(obj2, IDependencyObjectAccessor).InitializeDefinitionForRuntime(Me)
                    Me.DependencyPropertyValues.Item([property]) = obj2
                    [Continue] For
                End If
                If TypeOf obj2 Is WorkflowParameterBindingCollection Then
                    Dim list As IList = [TryCast](obj2,IList)
                    Dim i As Integer
                    For i = 0 To list.Count - 1
                        DirectCast(list.Item(i), IDependencyObjectAccessor).InitializeDefinitionForRuntime(Me)
                    Next i
                    Me.DependencyPropertyValues.Item([property]) = obj2
                    [Continue] For
                End If
                If TypeOf obj2 Is ActivityBind Then
                    Dim activity As Activity = Me.ResolveOwnerActivity
                    If (activity Is Nothing) Then
                        [Continue] For
                    End If
                    DirectCast(obj2, ActivityBind).SetContext(activity)
                End If
            Next
            Me.OnInitializeDefinitionForRuntime
            Me.InitializeProperties
            Me.Readonly = True
        End Sub

        Private Sub System.Workflow.ComponentModel.IDependencyObjectAccessor.InitializeInstanceForRuntime(ByVal workflowCoreRuntime As IWorkflowCoreRuntime) Implements IDependencyObjectAccessor.InitializeInstanceForRuntime
            Me.OnInitializeInstanceForRuntime(workflowCoreRuntime)
        End Sub


        ' Properties
        Friend ReadOnly Property DependencyPropertyValues As IDictionary([Of] DependencyProperty, Object)
            Get
                If (Me.dependencyPropertyValues Is Nothing) Then
                    Me.dependencyPropertyValues = New Dictionary([Of] DependencyProperty, Object)
                End If
                Return Me.dependencyPropertyValues
            End Get
        End Property

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Protected Friend ReadOnly Property DesignMode As Boolean
            Get
                Return Not Me.readonlyPropertyValue
            End Get
        End Property

        Friend ReadOnly Property MetaDependencyProperties As IList([Of] DependencyProperty)
            Get
                Return New List([Of] DependencyProperty)(Me.metaProperties.Keys).AsReadOnly
            End Get
        End Property

        Protected ReadOnly Property ParentDependencyObject As DependencyObject
            Get
                Return DirectCast(Me.GetValue(DependencyObject.ParentDependencyObjectProperty), DependencyObject)
            End Get
        End Property

        Friend Property [Readonly] As Boolean
            Get
                Return CBool(Me.GetValue(DependencyObject.ReadonlyProperty))
            End Get
            Set(ByVal value As Boolean)
                Me.SetReadOnlyPropertyValue(DependencyObject.ReadonlyProperty, value)
                Me.readonlyPropertyValue = value
            End Set
        End Property

        <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never), Browsable(False)> _
        Public Property Site As ISite
            Get
                Return DirectCast(Me.GetValue(DependencyObject.SiteProperty), ISite)
            End Get
            Set(ByVal value As ISite)
                Me.SetValue(DependencyObject.SiteProperty, value)
            End Set
        End Property

        <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(False)> _
        Public ReadOnly Property UserData As IDictionary
            Get
                Return DirectCast(Me.GetValue(DependencyObject.UserDataProperty), IDictionary)
            End Get
        End Property


        ' Fields
        Private dependencyPropertyValues As IDictionary([Of] DependencyProperty, Object)
        <NonSerialized> _
        Private metaProperties As IDictionary([Of] DependencyProperty, Object) = New Dictionary([Of] DependencyProperty, Object)
        Private Shared ParentDependencyObjectProperty As DependencyProperty = DependencyProperty.Register("ParentDependencyObject", GetType(DependencyObject), GetType(DependencyObject), New PropertyMetadata(Nothing, New Attribute() { New BrowsableAttribute(False), New DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden) }))
        Private Shared ReadonlyProperty As DependencyProperty = DependencyProperty.Register("Readonly", GetType(Boolean), GetType(DependencyObject), New PropertyMetadata((DependencyPropertyOptions.Metadata Or DependencyPropertyOptions.ReadOnly), New Attribute() { New BrowsableAttribute(False), New DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden) }))
        <NonSerialized> _
        Private readonlyPropertyValue As Boolean
        Private Shared SiteProperty As DependencyProperty = DependencyProperty.Register("Site", GetType(ISite), GetType(DependencyObject), New PropertyMetadata(DependencyPropertyOptions.Metadata, New Attribute() { New BrowsableAttribute(False), New DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden) }))
        Private Shared UserDataProperty As DependencyProperty = DependencyProperty.Register("UserData", GetType(IDictionary), GetType(DependencyObject), New PropertyMetadata((DependencyPropertyOptions.Metadata Or DependencyPropertyOptions.ReadOnly), New Attribute() { New BrowsableAttribute(False), New DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden) }))
    End Class
End Namespace

